package br.com.bomsabor.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import br.com.bomsabor.model.Caixa;
import br.com.bomsabor.model.Funcionario;
import br.com.bomsabor.model.Garcom;
import br.com.bomsabor.model.Gerente;

public class ImplFuncionarioDAO implements FuncionarioDAO {
	
	@Override
	public Garcom incluir(Garcom a) {
		String sql = "INSERT INTO Funcionario (nomeFunc, cpfFunc, salarioFunc, dataNascFunc, funcaoFunc)" +
		"VALUES (?, ?, ?, ?, ?)";
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			ps.setString(1, a.getNome());
			ps.setString(2, a.getCpf());
			ps.setDouble(3, a.getSalario());
			java.sql.Date dsql = new java.sql.Date(a.getNascimento().getTime());
			ps.setDate(4, dsql);
			ps.setString(5, "A");
			ps.executeUpdate();
			
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			a.setCodFuncionario(rs.getInt(1));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return a;
	}

	@Override
	public Caixa incluir(Caixa c) {
		String sql = "INSERT INTO Funcionario (nomeFunc, cpfFunc, salarioFunc, dataNascFunc, funcaoFunc)" +
		"VALUES (?, ?, ?, ?, ?)";
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			ps.setString(1, c.getNome());
			ps.setString(2, c.getCpf());
			ps.setDouble(3, c.getSalario());
			java.sql.Date dsql = new java.sql.Date(c.getNascimento().getTime());
			ps.setDate(4, dsql);
			ps.setString(5, "C");
			ps.executeUpdate();
			
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			c.setCodFuncionario(rs.getInt(1));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public Gerente incluir(Gerente g) {
		String sql = "INSERT INTO Funcionario (nomeFunc, cpfFunc, salarioFunc, dataNascFunc, funcaoFunc)" +
		"VALUES (?, ?, ?, ?, ?)";
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			ps.setString(1, g.getNome());
			ps.setString(2, g.getCpf());
			ps.setDouble(3, g.getSalario());
			java.sql.Date dsql = new java.sql.Date(g.getNascimento().getTime());
			ps.setDate(4, dsql);
			ps.setString(5, "G");
			ps.executeUpdate();
			
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			g.setCodFuncionario(rs.getInt(1));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return g;
	}
	
	public void alterar(Funcionario f) {
		String sql = "UPDATE Produto SET nomeFunc = ?, cpfFunc = ?, salarioFunc = ?, dataNascFunc = ?" +
		"WHERE codFunc = ?";       
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql);
		
			ps.setString(1, f.getNome());
			ps.setString(2, f.getCpf());
			ps.setDouble(3, f.getSalario());
			java.sql.Date dsql = new java.sql.Date(f.getNascimento().getTime());
			ps.setDate(4, dsql);
			ps.setLong(5, f.getCodFuncionario());
			ps.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void excluir(Funcionario f) {
		String sql = "DELETE Funcionario WHERE codFunc = ?";
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setLong(1, f.getCodFuncionario());
			ps.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Funcionario pesquisar(String nomeFuncionario) {
		String sql = "SELECT * FROM Funcionario WHERE UPPER(nomeFunc) LIKE'%' + UPPER(?) + '%'";
		Funcionario f = null;
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, nomeFuncionario);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				if(rs.getString("funcaoFunc") == "A"){
					f = new Garcom();
				}else if(rs.getString("funcaoFunc") == "C"){
					f = new Caixa();
				}else{
					f = new Gerente();
				}
			
				f.setCodFuncionario(rs.getLong("codFunc"));
				f.setNome(rs.getString("nomeFunc"));
				f.setCpf(rs.getString("cpfFunc"));
				java.util.Date dutil = new java.util.Date(rs.getDate("dataNascFunc").getTime());
				f.setNascimento(dutil);
				f.setSalario(rs.getDouble("salarioFunc"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return f;
	}

	public Funcionario pesquisar(long codFuncionario) {
		String sql = "SELECT * FROM Funcionario WHERE codFunc = ?";
		Funcionario f = null;
		Connection con = GenericDAO.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setLong(1, codFuncionario);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				
				if("A".equals(rs.getString("funcaoFunc"))){
					f = new Garcom();
				}else if("C".equals(rs.getString("funcaoFunc"))){
					f = new Caixa();
				}else{
					f = new Gerente();
				}
			
				f.setCodFuncionario(codFuncionario);
				f.setNome(rs.getString("nomeFunc"));
				f.setCpf(rs.getString("cpfFunc"));
				java.util.Date dutil = new java.util.Date(rs.getDate("dataNascFunc").getTime());
				f.setNascimento(dutil);
				f.setSalario(rs.getDouble("salarioFunc"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return f;
	}



	

}
